/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 10:53:19 AM
 * To change this template use File | Settings | File Templates.
 */
import dk.brics.automaton.Automaton;
import java.util.List;
import java.util.Set;
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Regexp, InternalSchemaErrorException, Schema, Context

class CharRegexp extends Regexp
{

    CharRegexp(Element element, Schema schema)
    {
        set = element.getAttributeValue("set");
        try
        {
            String s = element.getAttributeValue("min");
            if(s != null)
                min = new Character(s.charAt(0));
            s = element.getAttributeValue("max");
            if(s != null)
                max = new Character(s.charAt(0));
        }
        catch(IndexOutOfBoundsException indexoutofboundsexception)
        {
            throw new InternalSchemaErrorException();
        }
    }

    public Element toXML(Context context)
    {
        Element element = new Element("char", "http://www.brics.dk/DSD/2.0");
        if(set != null)
            element.setAttribute("set", set);
        if(min != null)
            element.setAttribute("min", min.toString());
        if(max != null)
            element.setAttribute("max", max.toString());
        return element;
    }

    boolean match(String s, Context context)
    {
        if(s.length() != 1)
            return false;
        char c = s.charAt(0);
        if(set != null && set.indexOf(c) == -1)
            return false;
        if(min != null && c < min.charValue())
            return false;
        return max == null || max.charValue() >= c;
    }

    Automaton toAutomaton(Context context)
    {
        if(set == null && min == null && max == null)
            return getAllchars();
        Automaton automaton;
        if(set != null)
            automaton = Automaton.makeCharSet(set);
        else
        if(min == null && max != null)
            automaton = Automaton.makeCharRange('\0', max.charValue());
        else
        if(min != null && max == null)
            automaton = Automaton.makeCharRange(min.charValue(), '\uFFFF');
        else
            automaton = Automaton.makeCharRange(min.charValue(), max.charValue());
        return automaton.intersection(getAllchars());
    }

    boolean isChar(Context context)
    {
        return true;
    }

    void getMentioned(Context context, Set set1, List list)
    {
        set1.add(null);
    }

    boolean checkSimple(Context context)
    {
        return true;
    }

    String set;
    Character min;
    Character max;
}
